import sys,os
from openpyxl import Workbook
import time

today=time.localtime()
year=today.tm_year
mon=today.tm_mon
day=today.tm_mday

date=f"{year}-{mon}-{day}"

fc=sys.argv[1]
d1={}
d2={}
with open(f"/data/share/{fc}/L01/summaryTable.csv") as f1,open(f"/data/mNGS/runmngs/GZ/{fc}/runStat.xls",encoding='gbk') as f2,open(f"/data/mNGS/runmngs/GZ/{fc}/fastq/SequenceStat.txt")as f3:
    for line in f1:
        line=line.strip()
        sq_items,sq_value=line.split(',')
        d1[sq_items]=str(sq_value)
    for l in f2:
        if '样本' in l:continue
        l=l.strip().split('\t')
        if l[0].find('-YF')>-1  or l[0].find('-SY') > -1 :continue
        d2[l[0]]=f'{l[16]}&{l[9]}'
    for k in f3:
        if 'TotalReads' in k:
            Base_t=k.split('\t')[1]
Base_t=f"{int(Base_t):,}"
Q30_t=d1['Q30(%)']
ESR_t=d1['ESR(%)']
FC=fc

wb=Workbook()
ws=wb.create_sheet('QC',0)
row_num=1
headers=["样本编号","Q30%（下机后总的）","Q30%（单个样本）","数据量（下机总数据量）","数据量（单个样本）","信噪比值（ESR值）","芯片号","报告日期"]
for p in range(0,len(headers)):
    cell=ws.cell(row=1,column=p+1,value=headers[p])
for i,j in d2.items():
    q30,base=j.split('&')
    row_num+=1
    wd=[i,Q30_t,q30,Base_t,base,ESR_t,FC,date]
    for p in range(0,len(wd)):
        cell=ws.cell(row=row_num,column=p+1,value=wd[p])

wb.save(f"/data/mNGS/runmngs/GZ/{fc}/QC_report_{fc}.xlsx")
